<template>
  <div>
    <el-table
      :data="store.getters['cate/getCate']"
      border
      style="width: 100%"
      row-key="id"
      default-expand-all
      :tree-props="{ children: 'children' }"
    >
      <el-table-column prop="id" label="分类编号" width="180" />
      <el-table-column prop="catename" label="分类名称" width="180" />
      <!-- <el-table-column label="图片">
        
      </el-table-column> -->
      <el-table-column label="图片">
        <template v-slot="item">
          <div>
            <!-- <img
                            v-if="item.row.img"
                            class="goodsImg"
                            :src="item.row.img"
                            alt="图片"
                        /> -->
            <el-image
              v-if="item.row.img"
              style="width: 60px; height: 60px"
              :src="item.row.img"
              :preview-src-list="[item.row.img]"
            >
            </el-image>
          </div>
        </template>
      </el-table-column>
      <el-table-column prop="status" label="状态">
        <template v-slot="item">
          <div>
            {{ item.row.status == 1 ? "启用" : "禁用" }}
          </div>
        </template>
      </el-table-column>
      <el-table-column label="操作">
        <template v-slot="item">
          <div>
            <el-button type="primary" size="small" @click="edit(item.row.id)"
              >编辑</el-button
            >
            <el-button type="danger" size="small" @click="del(item.row.id)"
              >删除</el-button
            >
          </div>
        </template>
      </el-table-column>
    </el-table>
  </div>
</template>

<script lang="ts" setup>
const tableData = [
  {
    date: "2016-05-03",
    name: "Tom",
    address: "No. 189, Grove St, Los Angeles",
  },
  {
    date: "2016-05-02",
    name: "Tom",
    address: "No. 189, Grove St, Los Angeles",
  },
  {
    date: "2016-05-04",
    name: "Tom",
    address: "No. 189, Grove St, Los Angeles",
  },
  {
    date: "2016-05-01",
    name: "Tom",
    address: "No. 189, Grove St, Los Angeles",
  },
];

import { catedelete } from "../../request/api";

import { defineEmits } from "vue";
import { useStore } from "vuex";

import { ElMessageBox, ElMessage } from "element-plus";

let store = useStore();

let emit = defineEmits<{
  (e: "edit", value: number): void;
}>();
// 封装一个编辑事件
let edit = (id: number) => {
  emit("edit", id);
};

let getCate = () => {
  store.dispatch("cate/getCateActions");
};

// console.log(store);

getCate();

// 删除事件
let del = (id: number) => {
  ElMessageBox.confirm("你确定要删除吗", "警告", {
    confirmButtonText: "确认",
    cancelButtonText: "取消",
    type: "warning",
  })
    .then(() => {
      catedelete({ id }).then((res) => {
        if (res.code == 200) {
          ElMessage.success(res.msg);
          getCate();
        } else {
          ElMessage.error(res.msg);
        }
      });
    })
    .catch(() => {
      ElMessage({
        type: "info",
        message: "删除取消",
      });
    });
};

</script>

<style lang="less" scoped></style>
